/** 
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
* 
* SdPMOStandardizationRecordService
* SD标准化人选档案
* 
* 修改纪录
* 2021-12-09 版本：1.0 jiangtao 创建。
* @version 版本：1.0
* @author 作者：jiangtao
* 创建日期：2021-12-09
*/
package com.topscomm.gpm.service.impl;

import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.gpm.pojo.GpmPMOStandardizationRecordEntity;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.pojo.GpmProjectReviewEntity;
import com.topscomm.tap.common.DatacodeCache;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.gpm.service.IGpmPMOStandardizationRecordService;
import com.topscomm.gpm.service.impl.auto.GpmPMOStandardizationRecordServiceAuto;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(value = "gpmPMOStandardizationRecordService")
@Scope(value = "singleton")
public class GpmPMOStandardizationRecordService extends GpmPMOStandardizationRecordServiceAuto implements IGpmPMOStandardizationRecordService {
	@Override
	public void setDispFields(Map<String, Object> mapRow, DatacodeCache datacodeCache) {
		super.setDispFields(mapRow,datacodeCache);
		String temp;
		if (mapRow.get("projectleveltype") != null) {
			temp = mapRow.get("projectleveltype").toString();
			mapRow.put("dispprojectleveltype", datacodeCache.getValueDynamic("GpmProjectLevelType", temp, "code", "name"));
		}
		// 可标准化省区
		if (mapRow.get("chargeprovinces") != null) {
			temp = mapRow.get("chargeprovinces").toString();
			StringBuffer stringBuffer = new StringBuffer();
			if(!StringUtil.isEmpty(temp)) {
				String[] array = temp.split(",");
				for(String a: array) {
					stringBuffer.append(datacodeCache.getValueDynamic("DeptCommonCode", a, "code", "name")).append(",");
				}
				if(stringBuffer.length()>0) {
					mapRow.put("dispchargeprovinces", stringBuffer.substring(0,stringBuffer.length()-1));
				}
			}
		}
		// 项目级别
		if (mapRow.get("projectlevel") != null) {
			temp = mapRow.get("projectlevel").toString();
			StringBuffer stringBuffer = new StringBuffer();
			if(!StringUtil.isEmpty(temp)) {
				String[] array = temp.split(",");
				for(String a: array) {
					stringBuffer.append(datacodeCache.getValueDynamic("GpmProjectLevel", a, "code", "name")).append(",");
				}
				if(stringBuffer.length()>0) {
					mapRow.put("dispprojectlevel", stringBuffer.substring(0,stringBuffer.length()-1));
				}
			}
		}
	}

	/**
	 * @description:查询当前用户是否为PMO(项目复盘用）
	 * @author: zhangjunyao
	 * @date: 2021-12-29
	 * @modify:
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean queryPMOPermission(Map<String, Object> paraMap) {
		boolean ispmo = false;
		StringBufferProxy sql = new StringBufferProxy();
		String userCode = paraMap.get("usercode").toString();
		if (!StringUtil.isEmpty(userCode)) {
			sql.appendSingle("usercode = '{0}'", userCode);
			List<GpmPMOStandardizationRecordEntity> pmoStandardizationRecordEntities = this.queryByWhere(sql.toString());
			if (!CollectionUtils.isEmpty(pmoStandardizationRecordEntities)) {
				ispmo = true;
			}
		}
		return ispmo;
	}

	/**
	 * @description:复盘PMO查看项目权限
	 * @author: zhangjunyao
	 * @date: 2021-12-29
	 * @modify:
	 */
	public boolean queryChargeProvinces(StringBufferProxy sqlWhere) {
		boolean isPmo = false;
		StringBufferProxy whereSql = new StringBufferProxy();
		whereSql.appendSingle("usercode = '{0}'", this.getSessionUserBean().getUser().getCode());
		List<GpmPMOStandardizationRecordEntity> pmoStandardizationRecordEntities = this.queryByWhere(whereSql.toString());
		if (CollectionUtils.isEmpty(pmoStandardizationRecordEntities)) {
			return isPmo;
		}
		isPmo = true;
		whereSql.clear();
		String chargeProvinces = pmoStandardizationRecordEntities.stream().map(entity -> entity.getChargeprovinces())
				.distinct().collect(Collectors.joining(","));
		String[] tempArr = chargeProvinces.split(",");
		chargeProvinces = new ArrayList<>(Arrays.asList(tempArr)).stream().distinct()
				.collect(Collectors.joining("','", "'", "'"));
		String projectLevels = pmoStandardizationRecordEntities.stream().map(entity -> entity.getProjectlevel())
				.distinct().collect(Collectors.joining(","));
		tempArr = projectLevels.split(",");
		projectLevels = new ArrayList<>(Arrays.asList(tempArr)).stream().distinct()
				.collect(Collectors.joining("','", "'", "'"));
		sqlWhere.appendSingle(" and {0}.{1} in ({2}) and {3}.{4} in ({5})", GpmProjectReviewEntity.tableName,
				GpmProjectReviewEntity.FieldDeptcommoncode1, chargeProvinces, GpmProjectEntity.tableName,
				GpmProjectEntity.FieldProjectlevel, projectLevels);
		return isPmo;
	}
}
